package com.lsa.southwind.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lsa.southwind.entity.Building;
import com.lsa.southwind.entity.Dormitory;
import com.lsa.southwind.entity.DormitoryAdmin;
import com.lsa.southwind.entity.Student;
import com.lsa.southwind.form.SearchForm;
import com.lsa.southwind.mapper.BuildingMapper;
import com.lsa.southwind.mapper.DormitoryAdminMapper;
import com.lsa.southwind.mapper.DormitoryMapper;
import com.lsa.southwind.mapper.StudentMapper;
import com.lsa.southwind.service.BuildingService;
import com.lsa.southwind.vo.BuildingVo;
import com.lsa.southwind.vo.PageVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author admin
 * @since 2023-02-25
 */
@Service
public class BuildingServiceImpl extends ServiceImpl<BuildingMapper, Building> implements BuildingService {
    @Autowired
    private BuildingMapper buildingMapper;
    @Autowired
    private DormitoryAdminMapper dormitoryAdminMapper;
    @Autowired
    private DormitoryMapper dormitoryMapper;
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageVo list(Integer page, Integer size) {
        //1.首先进行分页查询
        Page<Building> buildingPage = new Page<>(page, size);
        Page<Building> buildingList = this.buildingMapper.selectPage(buildingPage, null);

        List<BuildingVo> buildingVoList = new ArrayList<>();
        //2.把分页查询出来的数据进行遍历并赋值给为前端定义的BuildingVo
        for (Building building : buildingList.getRecords()) {
            BuildingVo buildingVo = new BuildingVo();
            BeanUtils.copyProperties(building, buildingVo);
            /*3.因为前端显示的是管理员的名字，而后端保存的是管理员的id，
             所以通过id查询出管理员的姓名并赋值给定义的VO
             */
            //4.通过id查询出所以管理员信息并把姓名赋值给前端
            DormitoryAdmin dormitoryAdmin = this.dormitoryAdminMapper.selectById(building.getAdminId());
            buildingVo.setAdminName(dormitoryAdmin.getName());
            buildingVoList.add(buildingVo);
        }
        PageVo pageVo = new PageVo();
        pageVo.setData(buildingVoList);
        pageVo.setTotal(buildingList.getTotal());
        return pageVo;
    }

    //搜索查询
    @Override
    public PageVo search(SearchForm searchForm) {
        Page<Building> buildingPage = new Page<>(searchForm.getPage(), searchForm.getSize());
        Page<Building> buildingPageList = null;
        if (searchForm.getValue().equals("")) {
            buildingPageList = this.buildingMapper.selectPage(buildingPage, null);
        } else {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.like(searchForm.getKey(), searchForm.getValue());
            buildingPageList = this.buildingMapper.selectPage(buildingPage, queryWrapper);
        }
        List<BuildingVo> buildingVoList = new ArrayList<>();
        for (Building building : buildingPageList.getRecords()) {
            BuildingVo buildingVo = new BuildingVo();
            BeanUtils.copyProperties(building, buildingVo);
            /*通过楼宇宿舍管理员id获取该管理员的名字*/
            DormitoryAdmin dormitoryAdmin = this.dormitoryAdminMapper.selectById(building.getAdminId());
            buildingVo.setAdminName(dormitoryAdmin.getName());
            buildingVoList.add(buildingVo);
        }
        PageVo pageVo = new PageVo();
        pageVo.setData(buildingVoList);
        pageVo.setTotal(buildingPageList.getTotal());
        return pageVo;
    }

    @Override
    public Boolean deleteById(Integer id) {
        //1.找到楼宇中的所有宿舍
        QueryWrapper<Dormitory> dormitoryQueryWrapper = new QueryWrapper<>();
        dormitoryQueryWrapper.eq("building_id", id);
        List<Dormitory> dormitoryList = this.dormitoryMapper.selectList(dormitoryQueryWrapper);
        for (Dormitory dormitory : dormitoryList) {
            //2.找到宿舍中的所有学生
            QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
            studentQueryWrapper.eq("dormitory_id", dormitory.getId());
            List<Student> studentList = this.studentMapper.selectList(studentQueryWrapper);
            for (Student student : studentList) {
                //3.给学生换宿舍
                /*
                 * 查出除了要被删除的宿舍外所有空余宿舍
                 * 然后把要被删除楼里宿舍的学生给他换过去
                 * 更新学生信息
                 * 更新宿舍信息
                 * */
                Integer availableDormitoryId = this.dormitoryMapper.findAvailableDormitoryId(id);
                student.setDormitoryId(availableDormitoryId);
                try {
                    this.studentMapper.updateById(student);
                    this.dormitoryMapper.subAvailable(availableDormitoryId);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            int dormitorydeleteById = this.dormitoryMapper.deleteById(dormitory.getId());
            if (dormitorydeleteById != 1) return false;

        }
        int buildingdelete = this.buildingMapper.deleteById(id);
        if (buildingdelete != 1) return false;

        return true;

    }

}
